﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Microsoft.Silverlight.Testing;
using Twilight.Controls;

namespace Twilight.Tests
{
    [TestClass]
    public class TweetInfoFixture : SilverlightTest
    {
        [TestMethod]
        public void DaysAgoAndFromLinkTest()
        {
            string from = "<a href=\"http://code.google.com/p/wittytwitter/\">Witty</a>";
            string created = FormatDateTimeToTwitterTime(DateTime.Now);

            TweetInfo ti = new TweetInfo();
            ti.From = from;
            ti.PostDate = created;

            // less than a minute ago from Witty

            Assert.AreEqual<int>(7, ti.Children.Count); // should be seven words
            Assert.AreEqual<Type>(typeof(HyperlinkButton), ti.Children[6].GetType());
            Assert.AreEqual<Uri>(new Uri("http://code.google.com/p/wittytwitter/"), ((HyperlinkButton)ti.Children[6]).NavigateUri);
        }


        [TestMethod]
        public void MinuteAgo()
        {
            string from = "<a href=\"http://code.google.com/p/wittytwitter/\">Witty</a>";
            string created = FormatDateTimeToTwitterTime(DateTime.Now.Subtract(TimeSpan.FromMinutes(1)));

            TweetInfo ti = new TweetInfo();
            ti.From = from;
            ti.PostDate = created;

            // 1 minute ago from Witty

            Assert.AreEqual<int>(5, ti.Children.Count); // should be five words
            Assert.AreEqual<string>("1 ", ((TextBlock)ti.Children[0]).Text);
            Assert.AreEqual<string>("minute ", ((TextBlock)ti.Children[1]).Text);
        }


        [TestMethod]
        public void MinutesAgo()
        {
            string from = "<a href=\"http://code.google.com/p/wittytwitter/\">Witty</a>";
            string created = FormatDateTimeToTwitterTime(DateTime.Now.Subtract(TimeSpan.FromMinutes(15)));

            TweetInfo ti = new TweetInfo();
            ti.From = from;
            ti.PostDate = created;

            // 15 minutes ago from Witty

            Assert.AreEqual<int>(5, ti.Children.Count); // should be five words
            Assert.AreEqual<string>("15 ", ((TextBlock)ti.Children[0]).Text);
            Assert.AreEqual<string>("minutes ", ((TextBlock)ti.Children[1]).Text);
        }


        [TestMethod]
        public void HourAgo()
        {
            string from = "<a href=\"http://code.google.com/p/wittytwitter/\">Witty</a>";
            string created = FormatDateTimeToTwitterTime(DateTime.Now.Subtract(TimeSpan.FromHours(1)));

            TweetInfo ti = new TweetInfo();
            ti.From = from;
            ti.PostDate = created;

            // 1 hour ago from Witty

            Assert.AreEqual<int>(5, ti.Children.Count); // should be five words
            Assert.AreEqual<string>("1 ", ((TextBlock)ti.Children[0]).Text);
            Assert.AreEqual<string>("hour ", ((TextBlock)ti.Children[1]).Text);
        }


        [TestMethod]
        public void HoursAgo()
        {
            string from = "<a href=\"http://code.google.com/p/wittytwitter/\">Witty</a>";
            string created = FormatDateTimeToTwitterTime(DateTime.Now.Subtract(TimeSpan.FromHours(5)));

            TweetInfo ti = new TweetInfo();
            ti.From = from;
            ti.PostDate = created;

            // 5 hours ago from Witty

            Assert.AreEqual<int>(5, ti.Children.Count); // should be five words
            Assert.AreEqual<string>("5 ", ((TextBlock)ti.Children[0]).Text);
            Assert.AreEqual<string>("hours ", ((TextBlock)ti.Children[1]).Text);
        }

        [TestMethod]
        public void DayAgo()
        {
            string from = "<a href=\"http://code.google.com/p/wittytwitter/\">Witty</a>";
            string created = FormatDateTimeToTwitterTime(DateTime.Now.Subtract(TimeSpan.FromHours(25)));

            TweetInfo ti = new TweetInfo();
            ti.From = from;
            ti.PostDate = created;

            // 1 day ago from Witty

            Assert.AreEqual<int>(5, ti.Children.Count); // should be five words
            Assert.AreEqual<string>("1 ", ((TextBlock)ti.Children[0]).Text);
            Assert.AreEqual<string>("day ", ((TextBlock)ti.Children[1]).Text);
        }


        [TestMethod]
        public void DaysAgo()
        {
            string from = "<a href=\"http://code.google.com/p/wittytwitter/\">Witty</a>";
            string created = FormatDateTimeToTwitterTime(DateTime.Now.Subtract(TimeSpan.FromHours(49)));

            TweetInfo ti = new TweetInfo();
            ti.From = from;
            ti.PostDate = created;

            // 2 days ago from Witty

            Assert.AreEqual<int>(5, ti.Children.Count); // should be five words
            Assert.AreEqual<string>("2 ", ((TextBlock)ti.Children[0]).Text);
            Assert.AreEqual<string>("days ", ((TextBlock)ti.Children[1]).Text);
        }

        private string FormatDateTimeToTwitterTime(DateTime dt)
        {
            dt = dt.ToUniversalTime();
            return string.Format("{0} +0000 {1}", dt.ToString("ddd MMM dd HH:mm:ss"), dt.Year);
        }
    }
}
